#!/usr/bin/env sh

. ${HOME}/.config/scripts/color/i3blocks-usage-color.sh

getCpuInfo() {
    cat /proc/stat | head -n 1 | awk '{print $5,($2+$3+$4+$5+$6+$7+$8+$9+$10+$11)}'
}

read -r IDLE1 TOTAL1 < <(getCpuInfo)
sleep 1
read -r IDLE2 TOTAL2 < <(getCpuInfo)

USAGE=$(echo "scale=0; 100-100*(${IDLE2}-${IDLE1})/(${TOTAL2}-${TOTAL1})" | bc)

ICON="\Uf4bc"

if [ "${USAGE}" -lt "40" ] ; then
    ICON_COLOR=${VERY_LOW_USAGE_COLOR}
elif [ "${USAGE}" -lt "60" ] ; then
    ICON_COLOR=${LOW_USAGE_COLOR}
elif [ "${USAGE}" -lt "80" ] ; then
    ICON_COLOR=${MIDDLE_USAGE_COLOR}
else
    ICON_COLOR=${HIGH_USAGE_COLOR}
fi

echo -e "${ICON} ${USAGE}%\n\n${ICON_COLOR}"

